package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.ICategoryBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 分类品牌 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2024-11-13
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements ICategoryBrandService {


    @Autowired
    private CategoryBrandMapper categoryBrandMapper;
    @Override
    public List<CategoryBrand> selectCategoryBrandList() {
        return categoryBrandMapper.selectCategoryBrandList();
    }

    @Override
    public int insertCategoryBrand(CategoryBrand categoryBrand) {
        LambdaQueryWrapper<CategoryBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId())
                .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId());
        long count = baseMapper.selectCount(queryWrapper);
        if(count > 0) {
            throw new ServiceException("该分类下已有该品牌");
        }
        return baseMapper.insert(categoryBrand);
    }
}
